//
//  YSSecurities.m
//  GuBoShi
//
//  Created by zhanglu on 2017/3/21.
//  Copyright © 2017年 YiShi. All rights reserved.
//

#import "YSSecurities.h"
#import <objc/runtime.h>

@interface YSSecurities ()

@end

@implementation YSSecurities

+ (NSDictionary *)JSONKeyPathsByPropertyKey
{
    return @{
             @"market":@"Exch",
             @"number":@"Inst",
             @"name":@"InstNm",
             @"type":@"ProTp",
             @"status":@"Stat",
             @"namePinYin":@"Py",
             @"minuteSpan":@"Decm",
             @"stall":@"Lev"
             
//             @"name":@"Instname",
//             @"number":@"Instid",
//             @"Id":@"Instcode"
             
             
//             @"Id":@"Ei"
             };
}

//InsertSecurities,item.Id,item.market,
//item.number,item.name,
//item.namePinYin,item.minuteSpan,@(item.stall),@(item.status),@(item.isSubscribe),item.isAdded];

//+ (NSValueTransformer *)typeJSONTransformer
//{
//    return [MTLValueTransformer reversibleTransformerWithBlock:^id(NSString * str) {
//        if ([str isEqualToString:@"STOCK_A"]) {
//            return @(YSSecuritiesTypeStock);
//        }
//        if ([str isEqualToString:@"ZS"]) {
//            return @(YSSecuritiesTypeMark);
//        }
//        return @(YSSecuritiesTypeFund);
//    }];
//}

+ (NSValueTransformer *)numberJSONTransformer
{
    return [MTLValueTransformer reversibleTransformerWithBlock:^id(NSNumber * num) {
        return [NSString stringWithFormat:@"%@",num];
    }];
}

- (NSString *)name
{
    if (!_name || !_name.length) {
        return [NSString stringWithFormat:@"%@",self.number];
    }
    return _name;
}

- (NSString *)namePinYin
{
    if (!_namePinYin || !_namePinYin.length) {
        return self.name;
    }
    return _namePinYin;
}

-(NSString *)isAdded
{
    if (!_isAdded) {
        _isAdded = @"0";
    }
    return _isAdded;
}

- (NSString *)IdStr{
    if (!_IdStr) {
        _IdStr = objectOrEmptyStr(self.Id).description;
    }
    return _IdStr;
}

- (void)updateWithDictionary: (NSDictionary *)dictionary {
    
    [[self quoteKeyMap] enumerateKeysAndObjectsUsingBlock:^(NSString  *_Nonnull key, NSString  *_Nonnull value, BOOL * _Nonnull stop) {
        id obj = [dictionary valueForKey:value];
        [self setValue:obj forKey:key];
    }];
    
    [[self qutoKeyMapToArray] enumerateKeysAndObjectsUsingBlock:^(NSString  *_Nonnull key, NSArray  *_Nonnull value, BOOL * _Nonnull stop) {
        
        id obj = [dictionary valueForKey:key]; //=>获取相应的array
        
        if ([obj isKindOfClass:[NSArray class]] && [((NSArray *)obj) count]) {
            [((NSArray *)obj) enumerateObjectsUsingBlock:^(id  _Nonnull subObj, NSUInteger idx, BOOL * _Nonnull stop) {
                if (idx <= value.count) {
                    [self setValue:subObj forKey:value[idx]];
                }
            }];
        }
    }];
}

- (BOOL)isValidID {
    return self.Id != nil && [self.Id isKindOfClass:[NSNumber class]];
}

+ (YSSecurities *)quoteWithDictionary: (NSDictionary *)dictionary{
    YSSecurities *quote = [YSSecurities new];
    [quote updateWithDictionary:dictionary];
    return quote;
}

- (void)updateWithQuote: (YSSecurities *)quote
{
    unsigned int iVarCount = 0;
    Ivar *iVarList = class_copyIvarList([self class], &iVarCount);//取得变量列表,[self class]表示对自身类进行操作
    for (int i = 0; i < iVarCount; i++) {
        Ivar var = *(iVarList + i);
        const char * varName = ivar_getName(var);//取得变量名字，将作为key
        NSString *key = [[NSString stringWithUTF8String:varName] substringFromIndex:1];
        //decode
        id varValue = [quote valueForKey:key];
        if (varValue && ![[self quoteKeyArray] containsObject:key]) {
            [self setValue:varValue forKey:key];
        }
    }
    free(iVarList);//记得释放内存
}

- (NSDictionary *)quoteKeyMap{
    return @{@"Id":@"Ei",
             @"TraDate":@"TraDate",//@"TraDate",//
             @"UpdTm":@"UpdTm",
             @"TotalTradeVolume":@"TotalVol",
             @"TotalTradeAmount":@"TotalTuov",
             @"LatestPx":@"LsPri",
             @"TuovRate":@"TuovRate",
             @"OpenInt":@"OpenInt",
             @"OpenPx":@"OpPri",
             @"HighPx":@"HiPri",
             @"LowPx":@"LoPri",
             @"PrevClosePx":@"PreClPri",
             @"PE":@"PE",
             @"PercentPx":@"AOI",
             @"BP":@"BP",
             @"SP":@"SP",
             @"BV":@"BV",
             @"SV":@"SV",
             @"Detai":@"Detai",
//             @"UpLiPrice":@"UpLiPrice",
//             @"LoLiPrice":@"LoLiPrice",
             @"PreClPri":@"PreClPri",
             @"ClPri":@"ClPri",
             @"CirAmt":@"CirAmt",
             @"TotAmt":@"TotAmt",
             @"CirVal":@"CirVal",
             @"TotVal":@"TotVal",
             @"S":@"S",
             @"B":@"B",
             @"ULP":@"ULP",
             @"LLP":@"LLP",
             @"SPT":@"SPT",
             @"AvgPx":@"AvPri"
             };
}


- (NSDictionary *)qutoKeyMapToArray {
    return @{@"BP":@[@"AskPx1",@"AskPx2",@"AskPx3",@"AskPx4",@"AskPx5"],
             @"BV":@[@"AskVolume1",@"AskVolume2",@"AskVolume3",@"AskVolume4",@"AskVolume5"],
             @"SP":@[@"BidPx1",@"BidPx2",@"BidPx3",@"BidPx4",@"BidPx5"],
             @"SV":@[@"BidVolume1",@"BidVolume2",@"BidVolume3",@"BidVolume4",@"BidVolume5"]};
}

- (NSArray *)quoteKeyArray{
    return @[@"Id",
             @"market",
             @"number",
             @"name",
             @"namePinYin",
             @"minuteSpan",
             @"stall",
             @"type",
             @"status",
             @"TraTimes",
             @"TraTimeTs",
             @"Decm",
             @"isSubscribe"];
}

- (NSDictionary *)jsonValue
{
    return [MTLJSONAdapter JSONDictionaryFromModel:self];
}

//- (NSNumber *)minuteSpan
//{
//    return @0;
//}

- (NSString *)openTime
{
    return @"20010101 09:30:00";
}

- (NSString *)endTime
{
    return @"20010101 15:00:00";
}

- (NSString *)restTime
{
    return @"20010101 11:30:00;20010101 13:00:00";
}

- (BOOL)hasVolume
{
    return YES;
}

@end
